﻿using System;
using System.Web.Mail;

namespace CMSModule
{
    public class EmailModule
    {
        #region 属性 字段

        /// <summary>
        /// 接收人地址
        /// </summary>
        private string _strSendToEmailAddress;
        public string StrSendToEmailAddress
        {
            get { return _strSendToEmailAddress; }
            set { _strSendToEmailAddress = value; }
        }
        /// <summary>
        /// 发件人地址
        /// </summary>
        private string _strEmailFromAddress;
        public string StrEmailFromAddress
        {
            get { return _strEmailFromAddress; }
            set { _strEmailFromAddress = value; }
        }
        /// <summary>
        /// 附件地址 （绝对地址）
        /// </summary>
        string _strAppendixPath;
        public string StrAppendixPath
        {
            get { return _strAppendixPath; }
            set { _strAppendixPath = value; }
        }
        /// <summary>
        /// 邮件标题
        /// </summary>
        string _strEmailTitle;
        public string StrEmailTitle
        {
            get { return _strEmailTitle; }
            set { _strEmailTitle = value; }
        }
        /// <summary>
        /// 邮件内容
        /// </summary>
        string _strEmailContent;
        public string StrEmailContent
        {
            get { return _strEmailContent; }
            set { _strEmailContent = value; }
        }
        /// <summary>
        /// 发送邮件账户
        /// </summary>
        string _strMyEmailAccount;
        public string StrMyEmailAccount
        {
            get { return _strMyEmailAccount; }
            set { _strMyEmailAccount = value; }
        }
        /// <summary>
        /// 发送邮件密码
        /// </summary>
        string _strMyPassword;
        public string StrMyPassword
        {
            get { return _strMyPassword; }
            set { _strMyPassword = value; }
        }
        /// <summary>
        /// 右键SMTP服务器
        /// </summary>
        string _strSmtpServer;
        public string StrSmtpServer
        {
            get { return _strSmtpServer; }
            set { _strSmtpServer = value; }
        }
        /// <summary>
        /// 是否使用第三方SMTP服务器
        /// </summary>
        bool _bolUseThird_partySMTPServer;

        public bool BolUseThird_partySMTPServer1
        {
            get { return _bolUseThird_partySMTPServer; }
            set { _bolUseThird_partySMTPServer = value; }
        }

        #endregion

        public EmailModule()
        { 
        }

        /// <summary>
        /// 邮件类初始化参数
        /// </summary>
        /// <param name="strEmailFromAddress">发件人地址</param>
        /// <param name="strSendToEmailAddress">接收人地址</param>
        /// <param name="strAppendixPath">附件地址 （绝对地址）</param>
        /// <param name="strEmailTitle">邮件标题</param>
        /// <param name="strEmailContent">邮件内容</param>
        /// <param name="strMyEmailAccount">发送邮件账户</param>
        /// <param name="strMyPassword">发送邮件密码</param>
        /// <param name="strSmtpServer">邮件SMTP服务器</param>
        public EmailModule(string strEmailFromAddress, string strSendToEmailAddress, string strAppendixPath, string strEmailTitle, string strEmailContent, string strMyEmailAccount, string strMyPassword, string strSmtpServer, bool bolUseThird_partySMTPServer)
        {
            StrEmailFromAddress = strEmailFromAddress;
            StrSendToEmailAddress = strSendToEmailAddress;
            _strAppendixPath = strAppendixPath;
            _strEmailTitle = strEmailTitle;
            _strEmailContent = strEmailContent;
            _strMyEmailAccount = strMyEmailAccount;
            _strMyPassword = strMyPassword;
            _strSmtpServer = strSmtpServer;
            _bolUseThird_partySMTPServer = bolUseThird_partySMTPServer;
        }

        public void SendEmail()
        {
            MailMessage objMailMessage;
            MailAttachment objMailAttachment;
            objMailMessage = new MailMessage();
            // 创建一个附件对象 
            if (!String.IsNullOrEmpty(_strAppendixPath))
            {
                objMailAttachment = new MailAttachment(_strAppendixPath);//发送邮件的附件 
                objMailMessage.Attachments.Add(objMailAttachment);//将附件附加到邮件消息对象中 
            }
            // 创建邮件消息 
            objMailMessage.From = StrEmailFromAddress;//源邮件地址 
            objMailMessage.To = StrSendToEmailAddress;//目的邮件地址，也就是发给我哈 
            objMailMessage.Subject = _strEmailTitle;//发送邮件的标题 
            objMailMessage.Body = _strEmailContent;//发送邮件的内容 
            objMailMessage.BodyFormat = MailFormat.Html;
            if (_bolUseThird_partySMTPServer == true)
            {
                //接着利用sina等第三方的SMTP来发送邮件，需要使用Microsoft .NET Framework SDK v1.1和它以上的版本 
                //基本权限 
                objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                //用户名 
                objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", _strMyEmailAccount);
                //密码 
                objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", _strMyPassword);
                //如果没有上述三行代码，则出现如下错误提示：服务器拒绝了一个或多个收件人地址。服务器响应为: 554 : Client host rejected: Access denied 
            }
            //SMTP地址 
            SmtpMail.SmtpServer = _strSmtpServer;
            //开始发送邮件 
            SmtpMail.Send(objMailMessage);
        }
    }

}
